Low-level MozJPEG bindings for Rust
This crate exposes the raw libjpeg API, so the libjpeg usage manual applies. You'll most likely want to use it via a higher-level API instead :)
Many fields in structs are marked as private by default, but if you need to access them, make a pull request marking them pub
.
Requirements
- build-essentials (gcc, etc.)
- nasm for Intel CPUs,
gas
for ARM. Note: Apple's Xcode ships an incredibly outdated version of nasm that won't work.
This crate supports x86, x86-64 and ARM64.
Usage
In Rust/Cargo, add "mozjpeg-sys" as a dependency in Cargo.toml
.
In case you need the jpeglib.h
header for C code built with Cargo, the required include paths (use env::split_paths()
) are set for Cargo build scripts in the DEP_JPEG_INCLUDE
env var.
For non-Rust projects you can build the library using Cargo:
It creates target/release/libmozjpeg_sys.a
and target/release/libmozjpeg_sys.{dll,so,dylib}
, which can be linked with C and other languages.
By default nasm_simd
feature is enabled, and this crate will try to compile SIMD support. Additionally, you can set TARGET_CPU
environmental variable (equivalent to -march=$TARGET_CPU
) to optimize all of C code for a specific CPU model.
Example
let mut err: jpeg_error_mgr = zeroed;
let mut cinfo: jpeg_decompress_struct = zeroed;
cinfo.common.err = jpeg_std_error;
jpeg_create_decompress;
let file_name = new.unwrap;
let mode = new.unwrap;
let fh = fopen;
jpeg_stdio_src;
jpeg_read_header;
// Available only after `jpeg_read_header()`
let width = cinfo.image_width;
let height = cinfo.image_height;
// Output settings be set before calling `jpeg_start_decompress()`
cinfo.out_color_space = J_COLOR_SPACEJCS_RGB;
jpeg_start_decompress;
let row_stride = cinfo.image_width as usize * cinfo.output_components as usize;
let buffer_size = row_stride * cinfo.image_height as usize;
let mut buffer = vec!;
while cinfo.output_scanline < cinfo.output_height
jpeg_finish_decompress;
jpeg_destroy_decompress;
fclose;
Writing:
let quality = 98;
let file_name = new.unwrap;
let mode = new.unwrap;
let fh = fopen;
let mut err = zeroed;
let mut cinfo: jpeg_compress_struct = zeroed;
cinfo.common.err = jpeg_std_error;
jpeg_create_compress;
jpeg_stdio_dest;
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.in_color_space = J_COLOR_SPACEJCS_RGB;
cinfo.input_components = 3;
jpeg_set_defaults;
let row_stride = cinfo.image_width as usize * cinfo.input_components as usize;
cinfo.dct_method = J_DCT_METHODJDCT_ISLOW;
jpeg_set_quality;
jpeg_start_compress;
while cinfo.next_scanline < cinfo.image_height
jpeg_finish_compress;
jpeg_destroy_compress;
fclose;